package lemsconfig;

# Perl Object Module to load in the details of the lems configuration	#
# file for use by the main program...					#

use XML::Parser;
use English;
use clutils qw(get_allocated_resource);
use Sys::Hostname;

sub new {
local $self;
local $__chardata;
local $action_list;
local $checkobject;
my $class=shift;
my $file=$_[0];
local $ARG_application=$_[1];

	print STDERR "File $file does not exist!\n" if ! -f $file;

	$self={};
	my $p = new XML::Parser(
			Handlers => { Start => \&handle_start,
					End => \&handle_end,
				       Char => \&handle_char },
			ErrorContext => 5
			);

	$p->parsefile($file);
	bless($self);
	return $self;
}

sub handle_start {
my $parser=shift;
my $element=shift;
my %attrs=(@_);
my @context=$parser->context;

	$element="\U$element";
	$__chardata="";

	if($element eq "GLOBALS") {
		if(!exists($attrs{modules}) ||
			!exists($attrs{programs}) ||
			!exists($attrs{logs})) {
			print STDERR "Warning: Attribute missing from globals\n";
			return;
		}
		$self->{GLOBALS}={};
		$self->{GLOBALS}->{MODULES}=$attrs{modules};
		$self->{GLOBALS}->{PROGRAMS}=$attrs{programs};
		$self->{GLOBALS}->{LOGS}=$attrs{logs};
		$self->{GLOBALS}->{PORT}=get_allocated_resource(hostname,"PORT",$ARG_application,"%%LEMS",undef);
		$self->{CHECKS}={};
		return;
	}
	if($element eq "CHECK") {
		$checkobject={};
		return;
	}
	if($element eq "ACTION_LIST") {
		$action_list={};
		return;
	}
	if($element eq "ACTION") {
		if(!exists($attrs{rc})) {
			print STDERR "Missing return code - line ignored\n";
			return;
		}
		if(exists($attrs{action})) {
			$action_list->{$attrs{rc}}=$attrs{action};
		} else {
			$action_list->{$attrs{rc}}="NOP";
		}
	}
}

sub handle_end {
my $parser=shift;
my $element=shift;

	#################################################################
	# We look for details on name, type, module,interval		#
	# action_list.							#
	#################################################################

	if(defined((grep(/^$element$/,qw(name type module interval)))[0])) {
		$element="\U$element";
		$checkobject->{$element}=$__chardata;
		$__chardata="";
		return;	
	}
	if($element eq "action_list") {
		$checkobject->{ACTIONS}=$action_list;
	}
	if($element eq "check") {
		$self->{CHECKS}->{$checkobject->{NAME}}=$checkobject;
	}
	$__chardata="";
}

sub handle_char {
my $parser=shift;
my $str=shift;

	$__chardata.=$str;
}

sub get_global {
my $self=shift;
my $global=shift;

	$global="\U$global";
	if(exists($self->{GLOBALS}->{$global})) {
		return $self->{GLOBALS}->{$global};
	}
	return undef;
}

sub get_check_list {
my $self=shift;

	return keys(%{$self->{CHECKS}});
}

sub check_exists {
my $self=shift;
my $check=shift;

	return exists($self->{CHECKS}->{$check});
}

sub get_check_field {
my $self=shift;
my $check=shift;
my $field=shift;

	if(exists($self->{CHECKS}->{$check}) &&
		exists($self->{CHECKS}->{$check}->{$field})) {
		return $self->{CHECKS}->{$check}->{$field};
	}
	return undef;
}

sub get_action {
my $self=shift;
my $check=shift;
my $action=shift;

	if(exists($self->{CHECKS}->{$check}) &&
		exists($self->{CHECKS}->{$check}->{ACTIONS}) &&
		exists($self->{CHECKS}->{$check}->{ACTIONS}->{$action})) {
		return $self->{CHECKS}->{$check}->{ACTIONS}->{$action};
	}
	return undef;
}

1;
